package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.*;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 查询今日运营数据
     *
     * @return vo
     */
    @Override
    public BusinessDataVO getBusinessData( LocalDateTime begin, LocalDateTime end ) {

        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        // 今日营业额 和 有效订单数
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        //查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        // 有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);
        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;

        if (totalOrderCount != 0 && validOrderCount != 0) {
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        // 新增用户数
        Integer newUsers = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return vo
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        return setmealMapper.getOverviewSetmeals();
    }

    /**
     * 菜品总览
     *
     * @return vo
     */
    @Override
    public DishOverViewVO overviewDishes() {
        return dishMapper.getOverviewDishes();
    }

    /**
     * 查询订单管理数据
     *
     * @return vo
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        // 获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        // 获得当天的结束时间
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        OrderOverViewVO overviewOrders = orderMapper.getOverviewOrders(begin, end);

        overviewOrders.setWaitingOrders(overviewOrders.getWaitingOrders() == null ? 0 : overviewOrders.getWaitingOrders());
        overviewOrders.setDeliveredOrders(overviewOrders.getDeliveredOrders() == null ? 0 : overviewOrders.getDeliveredOrders());
        overviewOrders.setCompletedOrders(overviewOrders.getCompletedOrders() == null ? 0 : overviewOrders.getCompletedOrders());
        overviewOrders.setCancelledOrders(overviewOrders.getCancelledOrders() == null ? 0 : overviewOrders.getCancelledOrders());
        return overviewOrders;
    }
}
